<?php

class Ms_Model_User extends Zend_Db_Table_Abstract
{
    protected $_name = "core_users";
    
    /**
     * 创建用户
     *
     * 接收用户提交的用户表单数据，将其写入数据表
     * 接收数据应是数组，须确保其键名与数据表中的字段对应
     *
     * @param array $userData
     */
    public function createUser($userData)
    {
        $row = $this->createRow();
        if (count($userData) > 0){
            foreach ($userData as $key=>$value){
                switch ($key) {
                    case 'password':
                        $row->$key = md5($value);
                        break;
                    case 'password2':
                        break;
                    default:
                        $row->$key = $value;
                }
            }
        }
        $row->role = 'user';
        $row->status = 1;
        $row->time_reg = time();
        $row->save();
        return $row->id;
    }
    
    /**
     * 获取用户
     *
     * 根据所传递的参数从数据库中查询用户
     *
     * @param int $id
     */
    public function getUser($where)
    {
        if (is_numeric($where)){
            $row = $this->find($where)->current();
        }
        if (is_array($where)){
            $select = $this->select();
            if (count($where) > 0){
                foreach ($where as $key=>$value){
                    $select->where($key." = ?", $value);
                }
            }
            $row = $this->fetchRow($select);
        }
        if ($row){
            return $row;
        }
        else{
            return null;
        }
    }
    
    /**
     * 获得用户列表
     *
     * @param array $where
     */
    public function getUsers($where = array())
    {
        $select = $this->select();
        if (count($where) > 0){
            foreach ($where as $key=>$value){
                $select->where($key . " = ? ", $value);
            }
        }
        $results = $this->fetchAll($select);
        if ($results) {
            return $results;
        }
        else{
            return null;
        }
    }
    
    /**
     * 登录时间
     *
     * 记录用户最后一次登录的时间
     *
     * @param int $id
     */
    public function loginTime($id){
        $row = $this->find($id)->current();
        if($row){
            $row->time_last = time();
            $row->save();
        }
        else{
            return FALSE;
        }
    }
    
    /**
     * 验证用户
     *
     * @param string $username
     */
    public function validUser($username)
    {
        $select = $this->select();
        $select->where("username = ?", $username);
        $result = $this->fetchRow($select);
        if ($result->username == $username){
            return $result->id;
        }
        else{
            return FALSE;
        }
    }


    /**
     * 编辑用户
     *
     * 编辑用户信息
     *
     * @param int $id
     * @param array $data
     * @param string $password
     */
    public function updateUser($id, $data, $password = null)
    {
        $row = $this->find($id)->current();
        if ($row){
            if (count($data) > 0){
                foreach ($data as $key=>$value){
                    // 如果原来有头像，重新赋值
                    if($row->avatar){
                        $oldAvatar = $row->avatar;
                    }
                    $row->$key = $value;
                    if($key == 'avatar' and $value != null){// 如果有上传头像
                        unlink('uploads/'.$oldAvatar); // 删除原来的头像
                        $row->avatar = date("Y-m")."/avatar/".$value;
                    }
                }
            }
            if ($password){
                $row->password = md5($password);
            }
            return $row->save();
            return $row->id;
        }
        else{
            return false;
        }
    }
    

    /**
     * 更改密码
     *
     * @param int $id
     * @param string $password
     * @throws Zend_Exception
     */
    public function changpassword($id, $password)
    {
        $row = $this->find($id)->current();
        if ($row){
            $row->password = md5($password);
            $row->save();
        }
        else {
            throw new Zend_Exception("用户不存在，更改密码未成功。");
        }
    }
    
    public function getTableName() {
        return $this->_name;
    }
}

